package my.soft.aiteacher.common.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import my.soft.aiteacher.exception.BusinessException;
import my.soft.aiteacher.common.mapStruct.UserMapStruct;
import my.soft.aiteacher.common.mapper.UserMapper;
import my.soft.aiteacher.common.pojo.UserDo;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserMapStruct userMapStruct;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDo user = userMapper.selectOne(new QueryWrapper<UserDo>().eq("username", username));
        if(Objects.isNull(user)){
            throw new BusinessException("用户不存在");
        }
        return userMapStruct.userDoToUserVo(user);

    }
}
